<template>
  <!--  <div class="login">-->
  <!--    <div class="login-box">-->

  <!--       <div class="login-box-content">-->
  <!--         <div class="login-icon">-->
  <!--           <img src="../assets/images/login-Icon.png" alt="">-->

  <!--         </div>-->
  <!--         <div class="login-box-title">智慧环保管理系统</div>-->
  <!--       </div>-->

  <!--      <div  class="login-box-sub-title">Pollution source collection and management system</div>-->
  <!--    </div>-->

  <!--    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">-->
  <!--      <h3 class="title">用户登录</h3>-->
  <!--      <el-form-item prop="username">-->
  <!--        &lt;!&ndash; <div class="account ">账号登录</div> &ndash;&gt;-->
  <!--        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入你的账号">-->
  <!--&lt;!&ndash;          <img src="../assets/images/user.png" alt="" class="icon-user">&ndash;&gt;-->

  <!--          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />-->
  <!--        </el-input>-->
  <!--      </el-form-item>-->
  <!--      <el-form-item prop="password">-->
  <!--        &lt;!&ndash; <div class="account ">密码登录</div> &ndash;&gt;-->
  <!--        <el-input-->
  <!--          v-model="loginForm.password"-->
  <!--          type="password"-->
  <!--          auto-complete="off"-->
  <!--          placeholder="请输入你的密码"-->
  <!--          @keyup.enter.native="handleLogin"-->
  <!--        >-->
  <!--          <img src="../assets/images/user.png" alt="" class="icon-user">2312-->
  <!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
  <!--        </el-input>-->
  <!--      </el-form-item>-->
  <!--      <el-form-item prop="code" v-if="captchaOnOff">-->
  <!--        <el-input-->
  <!--          v-model="loginForm.code"-->
  <!--          auto-complete="off"-->
  <!--          placeholder="验证码"-->
  <!--          style="width: 57%"-->
  <!--          @keyup.enter.native="handleLogin"-->
  <!--        >-->
  <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
  <!--        </el-input>-->
  <!--        <div class="login-code">-->
  <!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
  <!--        </div>-->
  <!--      </el-form-item>-->
  <!--      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px; float: left; margin-left: 28px; color: #fff;">记住密码</el-checkbox>-->
  <!--      <el-form-item style="width:100%;">-->
  <!--        <el-button-->
  <!--          :loading="loading"-->
  <!--          size="medium"-->
  <!--          type="primary"-->
  <!--          style="width:100%; font-size:30px;"-->
  <!--          @click.native.prevent="handleLogin"-->
  <!--        >-->
  <!--          <span v-if="!loading">登 录</span>-->
  <!--          <span v-else>登 录 中...</span>-->
  <!--        </el-button>-->
  <!--      </el-form-item>-->
  <!--    </el-form>-->
  <!--    &lt;!&ndash;  底部  &ndash;&gt;-->
  <!--    <div class="el-login-footer">-->
  <!--      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>-->
  <!--    </div>-->
  <!--  </div>-->


  <div class="login">
    <div class="login-box">

      <div class="login-box-content">
        <div class="login-icon">
          <img alt="" src="../assets/images/zhhb.png">

        </div>
        <div class="login-box-title" style="padding-left: 16px">智慧环保管理系统</div>
      </div>

      <div class="login-box-sub-title" style="margin-left: 50px; margin-top: 10px">Intelligent environmental management
        system
      </div>
    </div>

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">用户登录</h3>
      <el-form-item prop="username" >
        <!-- <div class="account ">账号登录</div> -->
        <el-input v-model="loginForm.username" auto-complete="off" placeholder="请输入你的账号" type="text">
          <!--          <img src="../assets/images/user.png" alt="" class="icon-user">-->

          <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="user"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" style="margin-top: 30px">
        <!-- <div class="account ">密码登录</div> -->
        <el-input
          v-model="loginForm.password"
          auto-complete="off"
          placeholder="请输入你的密码"
          type="password"
          @keyup.enter.native="handleLogin"
        >
          <img alt="" class="icon-user" src="../assets/images/user.png">2312
          <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="password"/>
        </el-input>
      </el-form-item>
      <el-form-item v-if="captchaOnOff" prop="code"  style="margin-top: 30px">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 57%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="validCode"/>
        </el-input>


        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode"/>
        </div>
      </el-form-item>
      <el-form-item style="height: 30px;margin-top: 30px " >
       <div class="login-z"
             style="text-align: right; color: #fff; width: 120px; height: 40px; float: right; margin-right: 26px; color: #fff;  text-align: center"
             @click="userLogin">
          企业用户注册
        </div>
        <el-checkbox v-model="loginForm.rememberMe"
                     style="margin:0px 0px 25px 0px; float: left; margin-left: 28px; color: #fff;">记住密码
        </el-checkbox>
      </el-form-item>
      <el-form-item style="width:100%; margin-top: 30px">
        <el-button
          :loading="loading"
          size="medium"
          style="width:100%; font-size:30px;"
          type="primary"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--    background-image:url(../assets/images/login-background.jpg)-->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body width="500px" custom-class="register">
      <el-form ref="loginForm2" :model="loginForm2" :rules="loginRules2" class="">
        <h3 class="title" style="color:#F3F3F3;">企业用户注册</h3>
        <el-form-item prop="username"  style="margin-top: 30px">
          <!-- <div class="account ">账号登录</div> -->
          <el-input v-model="loginForm2.username2" auto-complete="off" placeholder="请输入你的账号" type="text"  >
            <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="user"/>
          </el-input>


        </el-form-item>
        <el-form-item prop="password"   style="margin-top: 30px">
          <el-input
            v-model="newPassword2"
            auto-complete="off"
            placeholder="请输入你的密码"
            style="width:60%"
            type="password"
            @keyup.enter.native="handleLogin2"
          >
            <img alt="" class="icon-user" src="../assets/images/user.png">
            <svg-icon slot="prefix" class="el-input__icon input-icon" icon-class="password"/>
          </el-input>
          <div class="input_span">

            <span id="one">弱</span>
            <span id="two">中</span>
            <span id="three">强</span>
          </div>
        </el-form-item>

        <el-form-item  prop="code"  style="margin-top: 30px">
          <el-input
            v-model="loginForm2.password2 "
            auto-complete="off"
            placeholder="请再次输入你的密码"
            type="password"
            @keyup.enter.native="handleLogin2"
          >

            <!--            <img src="../assets/images/user.png" alt="" class="icon-user">-->
            <!--            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
          </el-input>


        </el-form-item>


        <el-form-item style="width:100%;margin-top: 30px" >
          <el-button
            :loading="loading"
            size="medium"
            style="width:100%; font-size:30px;"
            type="primary"
            @click.native.prevent="handleLogin2"
          >
            <span v-if="!loading">注册</span>
            <!--            <span v-else>登 录 中...</span>-->
          </el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2021  All Rights Reserved.</span>
    </div>
  </div>

</template>

<script>
import {getCodeImg, UserInfo} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      open: false,
      // admin
      // admin123
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },

      loginForm2: {
        username2: "",
        password2: "",
        rememberMe2: false,

      },
      newPassword2: "",
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        code: [{required: true, trigger: "change", message: "验证码不能为空"}]
      },
      loginRules2: {
        username2: [
          {required: true, trigger: "blur", message: "账号不能为空"}
        ],
        password2: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        newPassword2: [{required: true, trigger: "change", message: "密码不能为空"}]
      },
      loading: false,
      captchaOnOff: false,
      redirect: undefined
    };
  },
  watch: {
    newPassword2(newname, oldname) {
      this.msgText = this.checkStrong(newname);
      if (this.msgText > 1 || this.msgText == 1) {
        document.getElementById("one").style.background = "red";
      } else {
        document.getElementById("one").style.background = "#eee";
      }
      if (this.msgText > 2 || this.msgText == 2) {
        document.getElementById("two").style.background = "orange";
      } else {
        document.getElementById("two").style.background = "#eee";
      }
      if (this.msgText == 4) {
        document.getElementById("three").style.background = "#00D1B2";
      } else {
        document.getElementById("three").style.background = "#eee";
      }
    },

    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    //this.getCode();
    this.getCookie();
  },
  methods: {

    userLogin() {
      this.open = true;
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({path: this.redirect || "/"}).catch(() => {
            });
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },

    checkStrong(sValue) {
      var modes = 0;
      //正则表达式验证符合要求的
      if (sValue.length < 1) return modes;
      if (/\d/.test(sValue)) modes++; //数字
      if (/[a-z]/.test(sValue)) modes++; //小写
      if (/[A-Z]/.test(sValue)) modes++; //大写
      if (/\W/.test(sValue)) modes++; //特殊字符

      //逻辑处理
      switch (modes) {
        case 1:
          return 1;
          break;
        case 2:
          return 2;
          break;
        case 3:
        case 4:
          return sValue.length < 4 ? 3 : 4;
          break;
      }
      return modes;
    },

    handleLogin2() {

      this.$refs.loginForm2.validate(valid => {
        if (valid) {
          if (this.loginForm2.username2 == "") {
            this.$message('请输入账号');
            return
          }
          console.log(this.loginForm2.password2)

          if (this.newPassword2 == "") {
            this.$message('请输入密码');
            return
          }
          if (this.loginForm2.password2 == "") {
            this.$message('请再次输入密码');
            return
          }

          if (this.loginForm2.password2 !== this.newPassword2) {
            this.$message('两次密码不一致');
            return
          }
          UserInfo(this.loginForm2.username2, this.newPassword2).then(res => {
            console.log(res)
            this.$message(res.msg);
            this.open = false;
          })
          // this.$store.dispatch("Login", this.loginForm).then(() => {
          //     this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          // }).catch(() => {
          //     this.loading = false;
          //     if (this.captchaOnOff) {
          //         this.getCode();
          //     }
          // });
        }
      });
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" >
.el-input__icon {
  height: 50px;
}
.el-form-item{
  //margin-bottom:30px!important;
}
.login {

  /*display: flex;*/
  /*justify-content: center;*/
  /*align-items: center;*/
  width: 100%;
  min-height: 100%;
  // height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: 100% 100%;
  border: 1px solid transparent;

  .login-box {
    width: 657px;

    margin: 0 auto;
    margin-top: 120px;

    .login-box-sub-title {
      font-size: 22px;
      font-family: Source Han Sans CN;
      font-weight: 500;
      color: #FFFFFF;
      letter-spacing: 2px;

    }

    .login-box-content {
      display: flex;
      align-items: center;

      .login-box-title {
        font-size: 46px;
        font-family: Source Han Sans CN;
        font-weight: 500;
        color: #FFFFFF;
        letter-spacing: 2px;
        padding-left: 5px;
      }

    }

    .login-icon {
      width: 82px;
      height: 74px;
      margin-left: 28px;

      img {
        width: 100%;
      }
    }
  }

}

.title {
  text-align: center;
  font-size: 25px;
  font-family: Adobe Heiti Std;
  font-weight: normal;
  color: #FFFFFF;
  margin: 0;
  padding-top: 20px;
  margin-bottom: 30px;
  /*line-height: 27px;*/
  /*margin: 0px auto 30px auto;*/
  /*text-align: center;*/
  /*color: #707070;*/
}

.login-form {
  box-sizing: border-box;
  border-radius: 6px;
  background: #ffffff;
  width: 637px;
  height: 570px;
  text-align: center;
  padding: 25px 25px 5px 25px;
  margin: 0 auto;

  background: url("../assets/images/login-border.png");
  background-size: 100% 100%;

  .el-button--medium {
    height: 60px;
    width: 90% !important;
    margin: 0 auto;
  }

  .el-form-item__error {
    color: #ff4949;
    font-size: 12px;
    line-height: 2;
    padding-top: 4px;
    position: absolute;
    top: 100%;
    left: 30px;
  }

  .el-form-item {
    margin-bottom: 24px;
  }

  .account {
    text-align: left;
    font-size: 24px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #FFFFFF;
    width: 90%;
    margin: 0 auto;
  }

  .el-input {
    width: 90%;
    height: 38px;
    margin: 0 auto;
    display: inline-block;

    input {
      border: none;
      height: 59px;
      background: url("../assets/images/input-bg.png") !important;
      background-size: 100% 100%;
      font-size: 22px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #DCDCDC;
    }
  }

  .input-icon {
    height: 55px;
    width: 18px;
    margin-left: 2px;
    color: #34ABFF;
  }

  .icon-user {
    width: 24px;
    height: 26px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 55px;

  width: 132px;
}

.login-z:hover {
  cursor: pointer;
  position: relative;
  z-index: 10000;
  color: #fff;
  background-color: #1890ff;
}

#inputValue {
  width: 240px;
  margin-left: 20px;
  padding-left: 10px;
  border-radius: 3px;
}

.input_span {
  display: inline-block;
  width: 40%;
}

.input_span span {
  display: inline-block;
  /*width: 91px;*/
  /*height: 10px;*/
  background: #eee;
  line-height: 20px;
  color: #fff;
  width: 32%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  vertical-align: middle;
  background: #eee;


}

#one {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-right: 0px solid;
  margin-left: 10px;
  margin-right: 3px;

}

#two {
  border-left: 0px solid;
  border-right: 0px solid;
  margin-left: -5px;
  margin-right: 3px;
}

#three {
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  border-left: 0px solid;
  margin-left: -5px;
}

#font {
  width: 199px;
  display: flex;
  margin: 0 auto;
  justify-content: space-between;
}

#font span:nth-child(1) {
  color: red;
  /*margin-left: 80px;*/
}

#font span:nth-child(2) {
  color: orange;
  /*margin: 0 60px;*/
}

#font span:nth-child(3) {
  color: #00d1b2;
}

.register {
  width: 100%;
  height: 450px;
  /*min-height: 100%;*/
  // height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  /*background-size: 100% 100%;*/
  border: 1px solid transparent;
  .el-input {
    width: 100%;
    height: 38px;
    border-radius: 4px !important;
    margin: 0 auto;
    display: inline-block;

    input {
      border: none;
      height: 36px !important;
      //background: url("../assets/images/input-bg.png") !important;
      background-size: 100% 100%;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333;
    }
    input::-webkit-input-placeholder{

      color:#333;

      font-size:16px;



    }
  }
}

</style>
